package com.itheima.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IteratorDemo04 {
    public static void main(String[] args) {
        Collection<Student> collection = new ArrayList<>();
        Student stu1 = new Student("张三",18);
        Student stu2 = new Student("李四",20);
        Student stu3 = new Student("王五",22);

        collection.add(stu1);
        collection.add(stu2);
        collection.add(stu3);

        for (int i = 0; i < collection.size(); i++) {
            System.out.println(((ArrayList<Student>) collection).get(i));
        }
        System.out.println("----------------");


        Iterator<Student> it = collection.iterator();
        while (it.hasNext()){
            Student student = it.next();
            System.out.println(student);
        }
        System.out.println("----------------");

        for (Student student : collection) {
            System.out.println(student);
        }
    }
}

class Student{
    private String name;
    private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
